/***************************************************************************
 * Copyright 2012-2013 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.authorisationService.util;

import it.txt.ens.authorisationService.amqp.ManagedBroker;
import it.txt.ens.authorisationService.amqp.ManagedNamespace;
import it.txt.ens.authorisationService.amqp.ManagedVirtualHost;
import it.txt.ens.authorisationService.amqp.ManagedVirtualHost.Type;
import it.txt.ens.core.ENSAuthzServiceConnectionParameters;
import it.txt.ens.core.factory.ENSAuthzServiceConnectionParametersFactory;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

/**
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 *
 */
public class ConfigurationParser {

    private ENSAuthzServiceConnectionParameters ensAuthzServiceConnParams;
    private ConcurrentMap<String,ManagedVirtualHost> virtualHostMap;
    private ConcurrentMap<String,ManagedBroker> brokerMap;
    private ConcurrentHashMap<String, ManagedNamespace> namespaceMap;

    public ConfigurationParser (Properties config, ENSAuthzServiceConnectionParametersFactory connParamsSF) {
        this.parseNamespaceJSONArray(config.getProperty(PropertiesKeys.ENS_NAMESPACES));
        this.parseBrokerJSONArray(config.getProperty(PropertiesKeys.ENS_BROKER_SERVERS));
        this.parseVirtualHostJSONArray(config.getProperty(PropertiesKeys.ENS_VIRTUAL_HOSTS), connParamsSF, brokerMap);
    }
    

    private void parseNamespaceJSONArray (String jsonArray) {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray rawNamespaceList = parser.parse(jsonArray).getAsJsonArray();
        this.namespaceMap = 
                new ConcurrentHashMap<String, ManagedNamespace>(rawNamespaceList.size());
        ManagedNamespace namespace;
        for (int i = 0; i < rawNamespaceList.size(); i++) {
            namespace = gson.fromJson(rawNamespaceList.get(i), ManagedNamespace.class);
            namespaceMap.put(namespace.getNamespaceName(), namespace);
        }
    }

    private void parseBrokerJSONArray (String jsonArray) {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray rawBrokerList = parser.parse(jsonArray).getAsJsonArray();
        this.brokerMap = 
                new ConcurrentHashMap<String, ManagedBroker>(rawBrokerList.size());
        ManagedBroker broker;
        for (int i = 0; i < rawBrokerList.size(); i++) {
            broker = gson.fromJson(rawBrokerList.get(i), ManagedBroker.class);
            brokerMap.put(broker.getBrokerName(), broker);
        }
    }
    
    private void parseVirtualHostJSONArray (String jsonArray,
            ENSAuthzServiceConnectionParametersFactory factory, Map<String,ManagedBroker> brokerMap) {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray rawVirtualHostList = parser.parse(jsonArray).getAsJsonArray();
        this.virtualHostMap = 
                new ConcurrentHashMap<String, ManagedVirtualHost>(rawVirtualHostList.size());
        ManagedVirtualHost vHost;
        for (int i = 0; i < rawVirtualHostList.size(); i++) {
            vHost = gson.fromJson(rawVirtualHostList.get(i), ManagedVirtualHost.class);
            if (vHost.getVirtualHostType() == Type.Access) {
                ManagedBroker authzServiceBroker = brokerMap.get(vHost.getHostingBrokerName());
                this.ensAuthzServiceConnParams = factory.create(
                    vHost.getBrokerUsername(), vHost.getBrokerPassword(),
                    authzServiceBroker.getIpAddress(), authzServiceBroker.getTcpPortNumber(),
                    authzServiceBroker.getHttpPortNumber(),
                    vHost.getVirtualHostName(), vHost.getENSAuthenticationServiceQueueName(), false);
            } else            
                virtualHostMap.put(vHost.getVirtualHostName(), vHost);
        }
    }


    /**
     * @return the ensAuthzServiceConnParams
     */
    public ENSAuthzServiceConnectionParameters getEnsAuthzServiceConnParams() {
        return ensAuthzServiceConnParams;
    }


    /**
     * @return the virtualHostMap
     */
    public ConcurrentMap<String, ManagedVirtualHost> getVirtualHostMap() {
        return virtualHostMap;
    }


    /**
     * @return the brokerMap
     */
    public ConcurrentMap<String, ManagedBroker> getBrokerMap() {
        return brokerMap;
    }


    /**
     * @return the namespaceMap
     */
    public ConcurrentHashMap<String, ManagedNamespace> getNamespaceMap() {
        return namespaceMap;
    }
}
